package com.erbao.io.netty.refactor.rpc;

import com.erbao.io.netty.refactor.transport.MyPkgMsg;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

public class ResponseMappingCallBack {
    /*key requestId,*/
    static ConcurrentHashMap<Long, CompletableFuture<Object>> mapping = new ConcurrentHashMap<>();

    public static void addCallback(long requestId, CompletableFuture<Object> completableFuture) {
        mapping.put(requestId, completableFuture);
    }

    public static void runCallback(MyPkgMsg pkgMsg){
        long requestId = pkgMsg.getHeader().getRequestId();
        mapping.get(requestId).complete(pkgMsg.getContent().getResponseObj());
        removeCallback(requestId);
    }


    public static void removeCallback(long requestId){
        mapping.remove(requestId);
    }
}
